package cn.lixu._05tran;


import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 * @author lixu
 * @data 2022/08/18 0018 19:15:43
 * @param: null
 * @return: null
 * 延时消息
 **/
public class Producter {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //1.获取Producter对象
        TransactionMQProducer producer = new TransactionMQProducer("tran_producer_group");
        //2.设置nameserver地址值
        producer.setNamesrvAddr("localhost:9876");
        //指定事务监听器
        producer.setTransactionListener(new MyTranListener());
        //3.启动
        producer.start();
        //4.创建消息对象，设置消息
        // topic：消息主题 tags：此主题下的二级分类 body：消息的字节
        Message message = new Message("hello_topic", "hello", "这是事务消息".getBytes(StandardCharsets.UTF_8));
//        //4.1设置延迟消息级别
//        message.setDelayTimeLevel(2);
        // 4.2.发送消息
        SendResult send = producer.sendMessageInTransaction(message,"这是一条事务消息");
        // 4.3.打印消息发送结果
        System.out.println(send);
        //5.关闭
        producer.shutdown();

    }
}
